/**
 * TrashViewData:用于View展示使用的View
 */

package com.tools.cleanmaster.model;

import com.tools.cleanmaster.ui.view.trash.TrashApkGroup;
import com.tools.cleanmaster.ui.view.trash.TrashCacheGroup;
import com.tools.cleanmaster.ui.view.trash.TrashGroup;
import com.tools.cleanmaster.ui.view.trash.TrashMemGroup;
import com.tools.cleanmaster.ui.view.trash.TrashResidualGroup;
import com.tools.cleanmaster.ui.view.trash.TrashSysCacheGroup;
import com.tools.cleanmaster.model.item.TrashItem;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ouyang
 */
public class TrashViewData {
    private ArrayList<TrashGroup> mGroupList = null;

    /**
     * 只允许通过createTrashViewData来创建TrashViewData对象
     */
    private TrashViewData() {

    }

    /**
     * 创建一个用于view展示的TrashViewData
     * 
     * @param trashesData
     * @return
     */
    public static TrashViewData createTrashViewData(TrashesData trashesData) {
        if (trashesData == null) {
            return null;
        }
        TrashViewData trashViewData = new TrashViewData();
        trashViewData.fillTrashes(trashesData);
        return trashViewData;
    }

    private void fillTrashes(TrashesData trashesData) {
        mGroupList = prepareTrashGroups();
        Map<TrashType, List<TrashItem>> map = trashesData.getTrashesData();
        for (int i = mGroupList.size() - 1; i >= 0; i--) {
            TrashGroup group = mGroupList.get(i);
            group.fillTrashes(map);
            if (!group.shouldBeDisplayed()) {
                mGroupList.remove(i);
            }
        }
    }

    private ArrayList<TrashGroup> prepareTrashGroups() {
        ArrayList<TrashGroup> groupList = new ArrayList<TrashGroup>();
        groupList.add(new TrashCacheGroup());
        groupList.add(new TrashApkGroup());
        groupList.add(new TrashResidualGroup());
        groupList.add(new TrashSysCacheGroup());
        groupList.add(new TrashMemGroup());
        return groupList;
    }

    /**
     * 返回数据mGroupList
     * 
     * @return
     */
    public List<TrashGroup> getGroupList() {
        return mGroupList;
    }

    /**
     * 返回扫描垃圾总量
     */
    public long getTotalSize() {
        long totalSize = 0;
        for (TrashGroup group : mGroupList) {
            totalSize += group.getTotalSize();
        }
        return totalSize;
    }

    /**
     * 返回勾选中的垃圾总量
     */
    public long getSelectedSize() {
        long totalSize = 0;
        for (TrashGroup group : mGroupList) {
            totalSize += group.getSelectedSize();
        }
        return totalSize;
    }
}
